$(function() {
	//添加药品分类信息
	$.ajax({
		type: "post",
		url: "https://xcxgoods.juseyx.cn/categories/GetCategoryList",
		data: {
			categoryname: "",
			isenabled: 1
		},
		success: function(res) {
			console.log(res);
			category = res.data;
			$('#category').html('<option value="">-分类-</option>')
			for(var i = 0; i < res.data.length; i++) {
				$('#category').append('<option value="' + res.data[i].categoryid + '">' + res.data[i].categoryname + '</option>');
			}
		},
		error: function(err) {
			console.log(err)
		}
	});
	//所有产品信息
	$.ajax({
		type: "post",
		url:   "https://xcxgoods.juseyx.cn/products/GetYyReceptionGoods",
		data:{
			pagesize:  7,
			pageindex:  1,
			ReceptionSearch:  $('.p_name').val(),
			brandid:  null,
			isdrugstatus: "N",
			SortBy:   "addeddate",
			Desc:  true
		},
		success: function(res) {
			console.log(res);
			$('.p_list tbody').html('');
			$('.pageindex_p').val(1);
			if(res.data.length > 0) {
				for(var i = 0; i < res.data.length; i++) {
					$('.p_list tbody').append('<tr><td>' +
						res.data[i].productname + '</td><td><img  width="50px" height="50px" src="' +
						res.data[i].goodlistimageurl + res.data[i].goodlistimagename + '" alt=""></td><td>' +
						res.data[i].dosageformname + '</td><td>' +
						res.data[i].brandname + '</td><td>' +
						res.data[i].manufacturer + '</td>' +
						'<td><span class="checkmed text-center" img="'+res.data[i].goodlistimageurl + res.data[i].goodlistimagename+'" costprice="'+res.data[i].costprice+'" marketprice="'+res.data[i].marketprice+'" proid="' + res.data[i].productid + '" isdrugstatus="' + res.data[i].isdrugstatus + '">+</span></td></tr>')
				}
				$('.pagecount_p').text(Math.ceil(res.datacount / 7));
			} else {
				$('.p_list tbody').append('<tr><td colspan="6" class="text-center">未找到产品</td></tr>')
			}
			//确认选择商品
			$('.checkmed').on('click', function(e) {
				var lis = $('.midmed span b');
				var canpush = true;
				for(var i = 0; i < lis.length; i++) {
					if($(lis[i]).attr('pid') == $(e.target).attr('proid')) {
						canpush = false;
					}
				}
				if(canpush == false) {
					$('.midmed b[pid="' + $(e.target).attr('proid') + '"]').next().val(parseInt($('.midmed b[pid="' + $(e.target).attr('proid') + '"]').next().val()) + 1);
				} else {
					$('.midmed').append('<span><b pid="' + $(e.target).attr('proid') + '" isdrugstatus="' +
						$(e.target).attr('isdrugstatus') + '" marketprice="' +$(e.target).attr('marketprice') + '" img="'+$(e.target).attr('img')+'" costprice="'+$(e.target).attr('costprice')+'">' + $(e.target).parent().prev().prev().prev().prev().prev().text() +
						'</b><input type="number" min="1" value="1"><a class="btn delbtn">删除</a></span>');

				}
				$('.delbtn').on('click', function(e) {
					$(e.target).parent('span').remove();
				})
			});

		},
		error: function(err) {
			console.log(err)
		}
	});

	//子分类信息
	$('#category').change(function() {
		$('#p_store').html('<option value="">-子分类-</option>')
		for(var i = 0; i < category.length; i++) {
			if(category[i].categoryid == $('#category').val()) {
				if(category[i].children.length > 0) {
					for(var j = 0; j < category[i].children.length; j++) {
						$('#p_store').append('<option value="' + category[i].children[j].categoryid + '">' + category[i].children[j].categoryname + '</option>')
					}
				} else {
					$('#p_store').append('<option value=""></option>')
				}
			}
		}
	});
	//搜索产品
	$('.search_product').on('click', function() {
		$('.pageindex_p').val(1);
		selectpro();
	});
	$('.search-index_p').on('click', function() {
		selectpro();
	});
	$('.prepage_p').on('click', function(e) {
		if(parseInt($('.pageindex_p').val()) - 1 < 1) {

		} else {
			$('.pageindex_p').val(parseInt($('.pageindex_p').val()) - 1);
			selectpro();
		}

	});
	$('.nextpage_p').on('click', function(e) {
		if(parseInt($('.pageindex_p').val()) + 1 > parseInt($(e.target).parent().next().children().text())) {

		} else {
			$('.pageindex_p').val(parseInt($('.pageindex_p').val()) + 1);
			selectpro();
		}
		selectpro();
	});
})

function selectpro() {
	$.ajax({
		type:"post",
		url:"https://xcxgoods.juseyx.cn/products/GetYyReceptionGoods",
		dataType:"json",
		data:  {
			categoryid:  $('#p_store').val(),
			pagesize: 7,
			pageindex: $('.pageindex_p').val(),
			ReceptionSearch: $('.p_name').val(),
			brandid: "",
			isdrugstatus: "N",
			SortBy: "addeddate",
			Desc: true
		},
		success: function(res) {
			console.log(res);
			$('.p_list tbody').html('');
			if(res.data.length > 0) {
				for(var i = 0; i < res.data.length; i++) {
					$('.p_list tbody').append('<tr><td>' +
						res.data[i].productname + '</td><td><img width="50px" height="50px" src="' +
						res.data[i].goodlistimageurl + res.data[i].goodlistimagename + '" alt=""></td><td>' +
						res.data[i].dosageformname + '</td><td>' +
						res.data[i].brandname + '</td><td>' +
						res.data[i].manufacturer + '</td>' +
						'<td><span class="checkmed text-center" img="'+res.data[i].goodlistimageurl + res.data[i].goodlistimagename+'" costprice="'+res.data[i].costprice+'" marketprice="'+res.data[i].marketprice+'" proid="' + res.data[i].productid + '" isdrugstatus="' + res.data[i].isdrugstatus + '">+</span></td></tr>')
				}
				$('.pagecount_p').text(Math.ceil(res.datacount / 7));
			} else {
				$('.p_list tbody').append('<tr><td colspan="6" class="text-center">未找到产品</td></tr>')
			}
			//确认选择商品
			$('.checkmed').on('click', function(e) {
				var lis = $('.midmed span b');
				var canpush = true;
				for(var i = 0; i < lis.length; i++) {
					if($(lis[i]).attr('pid') == $(e.target).attr('proid')) {
						canpush = false;
					}
				}
				if(canpush == false) {
					$('.midmed b[pid="' + $(e.target).attr('proid') + '"]').next().val(parseInt($('.midmed b[pid="' + $(e.target).attr('proid') + '"]').next().val()) + 1);
				} else {
					$('.midmed').append('<span><b pid="' + $(e.target).attr('proid') + '" marketprice="' +$(e.target).attr('marketprice') + '" img="'+$(e.target).attr('img')+'" costprice="'+$(e.target).attr('costprice')+'" isdrugstatus="' +
						$(e.target).attr('isdrugstatus') + '">' + $(e.target).parent().prev().prev().prev().prev().prev().text() +
						'</b><input type="number" min="1" value="1"><a class="btn delbtn">删除</a></span>');

				}
				$('.delbtn').on('click', function(e) {
					console.log(e)
					$(e.target).parent('span').remove();
				})
			});

		},
		error: function(err) {
			console.log(err)
		}
	});
}
$('.surecheck').on('click', function() {
		let roomid = $(".username").attr("data-id");
	$('div[roomid="' + roomid + '"] .Scroll_area').html($('.midmed').html());
	var spanlist = $('.midmed span b');
	for(var i = 0; i < spanlist.length; i++) {
		$($('div[roomid="' + roomid + '"] .Scroll_area span b')[i]).next().val(parseInt($(spanlist[i]).next().val()));
	};
	if(spanlist.length>0){
		$('div[roomid="' + roomid + '"] .Order').css("display","block");
	}else{
		$('div[roomid="' + roomid + '"] .Order').css("display","none");
	}
	$('.delbtn').on('click', function(e) {
		console.log(e)
		$(e.target).parent('span').remove();
	})
});
$('.del-med').on('click', function() {
	$('.medic').html('');
	$('.midmed').html('');
})
$('.mis-div').on('click',function(){
	$('.zzzzc').hide();
});
